import regeneratorRuntime from '../../../utils/runtime.js';
import img_list from '../../../utils/util.js';
import { setTabBar } from '../../../utils/util'
import { getData, setData, clearData, dataKey } from '../../../utils/storage.js';
import shareFun from '../../../utils/shareFun.js';
import {
  forecast,
  getGrowth,
  checkHostReg,
  query_memberlevel,
  login
} from '../../../utils/imapi.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  In:true,
    is_host:0,
    userInfo:app.globalData.userInfo,
    info_top:app.globalData.CustomBar,
    margin_top:(456*app.globalData.screenWidth/750) - app.globalData.CustomBar - 14,
    show:1,
    img_list:"img_list.img_list",
    is_audit:1,
    login_exit_text:'登陆',
    result: '',
    bottom:app.globalData.isIphonex==1?'100rpx':'10rpx',
  },

  /**
   * 生命周期函数--监听页面加载； 只会调用一次
   */
  onLoad:async function (options) {
    this.setData({
      img_list:img_list.img_list,
      
    })
    shareFun.hidenShareMenu();
    /*if(app.globalData.uid == '0' || app.globalData.uid==''){
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }*/
    if(!app.is_auth()){
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }

   
   

  },

   /**
   * 生命周期函数--监听页面显示； 回到页面 或者页面暂时得时候调用 ；多次调用
   */
  onShow:async function () {
    setTabBar.call(this, 4)

    let that=this
    console.log('======userInfo:'+app.globalData.userInfo)
   this.setData({
    is_audit:app.globalData.is_audit,
    userInfo:app.globalData.userInfo,
   })
    

    if(!app.is_auth()){
      this.setData({login_exit_text:'登陆'})

      wx.showToast({
        title: '您离线了，请登陆哦',
      })

      return;
    }
    this.setData({login_exit_text:'退出'})




    const {data} = await checkHostReg()

    this.setData({
      is_host:data==3?1:0,//
      // userInfo:app.globalData.userInfo
    })


    const forecast_data= await forecast()
    this.setData({
      foreData:forecast_data.data
    })


    const growThData = await getGrowth();
    this.setData({
      width:growThData.data.total_value/3500,
      total_value:growThData.data.total_value
    })


    // 检测level_code 变动过； 如果变动了，那么重新登陆即可

    const res_level=await query_memberlevel()
    if(app.globalData.userInfo.level_code!=res_level.data){
       // 重新登陆一次 

       wx.login({
        success: async ({ code }) => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          const rdata = await login(code);
          if (rdata.code == '0') {
            await setData(dataKey.token, rdata.data.access_token);
            wx.setStorageSync('token', rdata.data.access_token)
            await setData(dataKey.session_key, rdata.data.session_key);
            app.globalData.session_key = rdata.data.session_key


            await setData(dataKey.userInfo, rdata.data)
            app.globalData.userInfo = rdata.data;
            
            that.setData({
              userInfo:app.globalData.userInfo,
            })
            
          
          }
        }
      })


     
    }

  },
  closeIn(){
  	  this.setData({
  		  In:false
  	  });
  },
  goBaidu(){
	  wx.navigateTo({
	    url: '/packageTab/pages/Externalinks/Externalinks',
	  })
	},
  onPageScroll: function (e) {
    var that = this;
    this.scrollTop = e.scrollTop
    if(e.scrollTop>100 && this.data.cur_scroll_top==100) return;
    that.setData({
      cur_scroll_top:e.scrollTop>100?100:e.scrollTop
    })
 },
  earningForm(){
    wx.navigateTo({
      url: '/pages/module/earnForm/earnForm',
    })
  },
  // sao(){
	 //  var _this = this;
	      
	 //      wx.scanCode({
	 //        success: (res) => {
		// 		console.log(res);
	 //          var result = res.result;
	   
	 //          _this.setData({
	 //            result: result,
	   
	 //          })
	 //        }
	 //      })

  // },
  toLogin(){
    console.log("====:",app.is_auth())
    if(!app.is_auth()){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  growth(){
    wx.navigateTo({
      url: '/pages/module/growth/growth',
    })
  },
  withdrawal(){
    wx.navigateTo({
      url: '/pages/module/withdrawal/withdrawal',
    })
  },
  rebate(){
    wx.navigateTo({
      url: '/pages/module/rebate/rebate',
    })
  },
  xpOrder(){
    wx.navigateTo({
      url: '/pages/module/xpOrder/xpOrder',
    })
  },
  shopCar(){
    wx.navigateTo({
      url: '/pages/module/shopCar/shopCar',
    })
  },
  securities(){
    wx.navigateTo({
      url: '/packageTab/pages/securities/securities',
    })
  },
  myFans(){
    wx.navigateTo({
      url: '/pages/module/addressBook/addressBook',
    })
    // wx.navigateTo({
    //   url: '/pages/module/myFans/myFans',
    // })
  },
  opinion(){
    wx.navigateTo({
      url: '/packageTab/pages/opinion/opinion',
    })
  },
  about(){
    wx.navigateTo({
      url: '/packageTab/pages/about/about',
    })
  },
  visit(){
    wx.navigateTo({
      url: '/packageTab/pages/visit/visit',
    })
  },
  addred(){


    wx.showToast({
      title: '即将上线',
    })
    return 
	// wx.navigateTo({
	//   url: '/packageA/pages/TheActivation/TheActivation',
	// })
	wx.navigateTo({
	  url: '/packageA/pages/assistant/assistant',
	})
  },
  address(){
    wx.navigateTo({
      url: '/pages/module/addAddress/addAddress',
    })
  },
  hostApply(){
    wx.navigateTo({
      url: '/pages/module/hostApply/hostApply',
    })
  },
  goHost(){
    wx.navigateTo({
      url: '/pages/module/liveManagement/liveManagement',
    })
  },
  founder(){
	wx.navigateTo({
	  url: '/packageTab/pages/founder/founder',
	})
  },
  // ascension(){
	 //  wx.navigateTo({
	 //    url: '/packageTab/pages/ascension/ascension',
	 //  })
  // },
  async applyHost(e){
    var that = this;
    var index = e.currentTarget.dataset.index
  },
  serveDesc(e){
    var index = e.currentTarget.index;
    wx.navigateTo({
      url: '/packageTab/pages/rookie/rookie?index=' + index,
    })
  },
  setting(){
    wx.navigateTo({
      url: '/pages/module/setting/setting',
    })
  },
  exit(){


    if(!app.is_auth()){
      var pages = getCurrentPages() //获取加载的页面
      var currentPage = pages[pages.length-1] //获取当前页面的对象
      var url = currentPage.route //当前页面ur
      
      wx.navigateTo({
        url: '/pages/login/login?redirect_url='+url+'&nav_type=tab',
      })
      return 
    }

    wx.showModal({
      title: '您确定要退出登录吗？',
      content: '',
      cancelText:'取消',
      confirmText:'确认',
      confirmColor:'#F95A4B',
      success:async (res)=> {
        if (res.confirm) {//取消
          await clearData()
          app.globalData.is_need_reload = true
          app.globalData.userInfo={}
          this.setData({
            userInfo:{}
          })

          var pages = getCurrentPages() //获取加载的页面
          var currentPage = pages[pages.length-1] //获取当前页面的对象
          var url = currentPage.route //当前页面ur
          
          wx.navigateTo({
            url: '/pages/login/login?redirect_url='+url+'&nav_type=tab',
          })
        } else if (res.cancel) {//确认
         
        }
      }
    })
  },
 

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // this.setData({
    //   hasData:0
    // })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  // 下拉加载
  onPullDownRefresh:async function () {
    wx.showNavigationBarLoading();
    setTimeout(() => {
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    }, 500);
    if(app.globalData.uid == '0' || app.globalData.uid==''){
      return;
    }
    const {data} = await checkHostReg()
    if(!app.globalData.userInfo){
      try {
        let userInfo = await getData(dataKey.userInfo);
        app.globalData.userInfo = userInfo;
      } catch (error) {
        
      }
    }
    this.setData({
      is_host:data==3?1:0,
      userInfo:app.globalData.userInfo
    })
    const growThData = await getGrowth();
    this.setData({
      width:growThData.data.total_value/3500,
      total_value:growThData.data.total_value
    })

    const {data_forecast} = await forecast();
    this.setData({
      foreData:data_forecast
    })
   

  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function () {

  // },

})